﻿
; (function (root) {
    var app = root.app = root.app || {};
   

    //uncomment to use jQuery AJAX calls
    var $ = root.jQuery;


    // add members to the dataservice
    var dataservice = {
        getToasts: getToasts,
        getSubmissions: getSubmissions,
        updateSubmissions: updateSubmissions
    };

    // extend the app with this dataservice
    app.dataservice = dataservice;


    ////jaydata stack: not really necessary in such a simple app
    ////but will be very useful for offline apps
    ////if used for online apps will need to mantain remote and local repository
    //var Toast = $data.define("Toast", {
    //    Id:Number,
    //    Category: String,
    //    Content: String,
    //    Author: String,
    //    Website: String 

    //});

    //var ToastDB = $data.EntityContext.extend("ToastDB", {
    //    Toasts: { type: $data.EntitySet, elementType: Toast }
    //});

    //Toast.storeToken = {
    //    factory: function () {
    //        return new ToastDB({ name: "webApi", apiUrl: "/api" })
    //    }
    //};

    function getToasts(callback) {

        // Toast.readAll().then(callback);
        //the easy way which would work here just as well for simple queries

        $.get('/api/Toasts', callback);
    }

    function getSubmissions(callback) {
        $.get('/api/Submissions', callback);
    }



    function updateSubmissions(data) {
        $.ajax({
            url: '/api/Submissions',
            cache: false,
            type: 'PUT',
            data: JSON.stringify(data),
            contentType: 'application/json; charset=utf-8',
            success: function () {
                window.location.href = "http://" + window.location.host;
            }

        });
    }


}(window));